package com.miracle.system.service.impl;

import java.util.*;

import com.miracle.system.domain.SysResource;
import com.miracle.system.domain.SysRole;
import com.miracle.system.domain.SysRoleResource;
import com.miracle.system.mapper.SysResourceMapper;
import com.miracle.system.mapper.SysRoleResourceMapper;
import com.miracle.system.mapper.SysRoleUserMapper;
import com.miracle.system.service.SysResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.miracle.common.utils.StringUtils;
import com.miracle.system.mapper.SysRoleMapper;
import com.miracle.system.service.SysRoleService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 角色 业务层处理
 *
 * @author miracle
 */
@Service
public class SysRoleServiceImpl implements SysRoleService
{
    @Resource
    SysRoleMapper roleMapper;
    @Resource
    SysRoleResourceMapper roleResourceMapper;
    @Resource
    SysRoleUserMapper roleUserMapper;
    @Resource
    SysResourceMapper resourceMapper;
    @Autowired
    SysResourceService resourceService;

    @Override
    public List<SysRole> selectRoleList(SysRole role) {
        return roleMapper.selectRoleList(role);
    }

    @Override
    @Transactional
    public Integer insertRole(SysRole role) {

        Integer rows = roleMapper.insertRole(role);
        insertRoleResource(role);

        return rows;
    }

    @Override
    @Transactional
    public Integer deleteRoleByRoleIds(Integer[] roleIds) {

        roleUserMapper.deleteRoleUserByRoleIds(roleIds);
        roleResourceMapper.deleteRoleResourceByRoleIds(roleIds);

        return roleMapper.deleteRoleByRoleIds(roleIds);
    }

    @Override
    @Transactional
    public Integer updateRole(SysRole role) {

        Integer rows = roleMapper.updateRole(role);
        updateRoleResource(role);

        return rows;
    }

    @Override
    public SysRole remoteCheckRole(SysRole role) {
        return roleMapper.remoteCheckRole(role);
    }

    @Override
    public SysRole selectRoleByRoleId(Integer roleId) {
        SysRole role = roleMapper.selectRoleByRoleId(roleId);
        role.setRoleResources(resourceService.selectResourceByRoleId(roleId));
        return role;
    }

    private void insertRoleResource(SysRole role){
        List<SysResource> resources = role.getRoleResources();

        if (resources.size() > 0){
            List<SysRoleResource> roleResourceList = new ArrayList<SysRoleResource>();
            for (SysResource resource : resources) {
                SysRoleResource roleResource = new SysRoleResource();
                roleResource.setRoleId(role.getRoleId());
                roleResource.setResourceId(resource.getResourceId());
                roleResource.setCreateBy(role.getCreateBy());
                roleResourceList.add(roleResource);
            }
            roleResourceMapper.batchInsertRoleResource(roleResourceList);
        }
    }

    private void updateRoleResource(SysRole role){
        List<SysResource> newResourceList = role.getRoleResources();
        if (newResourceList.size() > 0){
            roleResourceMapper.deleteRoleResourceByRoleIdAndNoResource(role.getRoleId(), newResourceList);

            insertRoleResource(role);
        }
    }
}
